package com.shop.spring.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {

    private static Map<String, SimpleDateFormat> hashMap = new HashMap<>();

    static {
        hashMap.put("yyyyMMdd",new SimpleDateFormat("yyyyMMdd"));
        hashMap.put("yyyyMMdd HH:mm:ss",new SimpleDateFormat("yyyyMMdd HH:mm:ss"));
    }

    public static Date parseDate(String dateStr , String format){
        SimpleDateFormat simpleDateFormat = hashMap.get(format);
        if (simpleDateFormat == null)
            simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            System.out.println(e);
        }
        return null;
    }

    public static void main(String[] args) {

        System.out.println(parseDate("20191219 21:22:00","yyyyMMdd HH:mm:ss"));
    }
}
